package itsm.isperp.module.service.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.commons.repository.BaseRepository;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.message.domain.IMessagePacket;
import itsm.isperp.framework.message.service.MessageQueueService;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppMessage;
import itsm.isperp.module.entity.app.AppMessageSuccess;
import itsm.isperp.module.repository.app.AppMessageMapper;
import itsm.isperp.module.repository.app.AppMessageSuccessMapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class AppMessageService extends BaseService<AppMessage> implements
		MessageQueueService {

	@Autowired
	protected AppMessageMapper appMessageMapper;

	@Autowired
	protected AppMessageSuccessMapper appMessageSuccessMapper;

	@Override
	public BaseRepository<AppMessage> getRepository() {
		return this.appMessageMapper;
	}

	/**
	 * 自增长1
	 */
	public void failCountIncrease(String id) {
		this.appMessageMapper.failCountIncrease(id);

	}

	/**
	 * 查询所有
	 * 
	 * @return
	 */
	public List<AppMessage> findAllMessage() {
		return this.appMessageMapper.findAllMessage();
	}

	public IUser getSender(String id) {

		DataResponse<AppMessageSuccess> result = this.appMessageSuccessMapper
				.findAllByRequest(new JqGridRequest().isPageable(false)
						.setSelectColumns("sender_id")
						.addFilterRule("id", JqGridFilterRuleOp.eq, id));
		if (result.getContent() != null && result.getContent().size() == 1) {
			AppUserService appUserService = ContextHolder
					.getSpringBean("appUserService");
			return appUserService.findByUsername(result.getContent().get(0)
					.getSenderId());

		}
		return null;
	}

	@Override
	public <T extends IMessagePacket> void addToMessageQuery(T message) {
		this.saveOrUpdate(new AppMessage(message));
	}

	@Override
	public <T extends IMessagePacket> void addToMessageQuery(
			Collection<T> messages) {

		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");

		List<Object[]> batchArgs = new ArrayList<>();
		String creator, creatorName;
		IUser u = ContextHolder.getLoginUser();
		if (u != null) {
			creator = u.getUsername();
			creatorName = u.getFullname();
		} else {
			creator = "admin";
			creatorName = "系统管理员";
		}

		for (IMessagePacket m : messages) {
			Object[] arg = new Object[16];
			arg[0] = HibernateUUIDGenerator.generate();
			arg[1] = m.getReceiver();
			arg[2] = m.getReceiverAddress();
			arg[3] = m.getTitle();
			arg[4] = m.getContent();
			arg[5] = m.getSender();
			arg[6] = m.getMessageType();
			arg[7] = 0;
			arg[8] = 0;
			arg[9] = m.getSourceId();
			arg[10] = creator;
			arg[11] = creatorName;
			arg[12] = new Date();
			arg[13] = creator;
			arg[14] = creatorName;
			arg[15] = new Date();
			batchArgs.add(arg);
		}

		jdbcTemplate
				.batchUpdate(
						"INSERT INTO app_message(id,receiver_id,receiver_address,title,content,sender_id,message_type,message_level,fail_count,source_id,creator,creator_name,create_date,modifier,modifier_name,modify_date)"
								+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
						batchArgs);

	}
}